import axios from "axios";
import {constants} from "./index";
import {fromJS} from "immutable";

export function toggleTopShow(flag) {
    return {
        type:constants.TOGGLE_SCROLL_TOP,
        show: fromJS(flag)
    }
}


const changeHomeData = (result) => ({
    type: constants.CHANGE_HOME_DATA,
    topicList: result.topicList,
    articleList: result.articleList,
    recommendList: result.recommendList
});
export const getHomeInfo = () => {
    return (dispatch) => {
        axios.get('http://localhost:3000/home').then((res) => {
            const result = res.data.data;
            dispatch(changeHomeData(result))
        }).catch((err) => {
            alert(err)
        })
    }
};
export const getMoreList = (page) => {
    return (dispatch) => {
        axios.get('http://localhost:3000/homeList?page=' + page).then((res) => {
            const result = res.data.data;
            dispatch(addHomeList(result, page + 1))
        });
    }
}
export const addHomeList = (list, page) => ({
    type: constants.ADD_ARTICLE_LIST,
    list: fromJS(list),
    page: fromJS(page)
})